package com.ie.service.admin;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.ConfigrarionBean;
import com.ie.common.security.utils.JwtTokenUtil;
import com.ie.common.security.utils.RedisUtil;
import com.ie.entity.AUserEntity;
import com.ie.mapper.admin.AUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AUserServiceImpl extends ServiceImpl<AUserMapper, AUserEntity> {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisUtil  redisUtil;
    @Autowired
    private ConfigrarionBean  Bean;

    @Override
    public ResponseResult<Map<String,String>> login(AUserEntity aUserEntity) {
    //获取参数
        Long count = lambdaQuery().eq(AUserEntity::getAname, aUserEntity.getAname()).count();
        if (count == 0) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        mapuser.put("aname",aUserEntity.getAname());
        mapuser.put("apwd",aUserEntity.getApwd());
        List<AUserEntity> list = query().allEq(mapuser).list();
        if (userlist.size() > 0) {
            jwtTokenUtil.createToken(aUserEntity.getAname());
            redisUtil.set("login_"+aUserEntity.getAname(),aUserEntity.getAname(),config.getRedisExpiration());
            //把生成的有效信息返回给ResponseResult
            HashMap<String,String> loginresult = new HashMap<>();
            loginresult.put("authtoken", token);
            loginresult.put("aname", aUserEntity.getAname());
            loginresult.put("aid", aUserEntity.getId()+"");
            return ResponseResult.getSuccessResult(loginresult);
        } else {
            return ResponseResult.getMessageResult(null, "A002");
        }
    }
}
